<template>
  <div>
    <el-main>
      <el-form
        ref="searchForm"
        size="small"
        :model="searchModel"
        :inline="true"
        @keyup.enter.native="search"
      >
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="订单编号">
              <el-input
                placeholder="请输入订单编号"
                v-model="searchModel.orderNo"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="会员名称">
              <el-input
                placeholder="请输入会员名称"
                v-model="searchModel.memberName"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="订单金额从">
              <el-input
                placeholder="请输入最低订单金额"
                v-model="searchModel.moneyStart"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="到">
              <el-input
                placeholder="请输入最高订单金额"
                v-model="searchModel.moneyEnd"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button icon="el-icon-refresh" size="small" @click="resetSearch"
            >重置
            </el-button>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="search"
            >搜索
            </el-button
            >
            <el-link type="primary" :underline="false" @click="searchAll = true" style="margin-left: 10px" v-if="!searchAll">展开<i class="el-icon-bottom el-icon--right"></i></el-link>
            <el-link type="primary" :underline="false" @click="searchAll = false" style="margin-left: 10px" v-if="searchAll">收起<i class="el-icon-top el-icon--right"></i></el-link>
          </el-col>
        </el-row>
        <el-row :gutter="20" v-if="searchAll">
          <el-col :span="4">
            <el-form-item label="支付金额从">
              <el-input
                placeholder="请输入最低支付金额"
                v-model="searchModel.payMoneyStart"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="到">
              <el-input
                placeholder="请输入最高支付金额"
                v-model="searchModel.payMoneyEnd"
                clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="支付方式">
              <el-select v-model="searchModel.payWay" placeholder="请选择支付方式" clearable>
                <el-option :value="1" label="微信"></el-option>
                <el-option :value="2" label="余额"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="支付时间">
              <el-date-picker
                v-model="searchModel.payTime"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始支付时间"
                end-placeholder="结束支付时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" v-if="searchAll">
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="searchModel.status" placeholder="请选择状态" clearable>
                <el-option :value="2" label="已支付"></el-option>
                <el-option :value="1" label="未支付"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="searchModel.createTime"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始创建时间"
                end-placeholder="结束创建时间"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        ref="table"
        :data="tableData.records"
        :border="true"
        style="width: 100%"
        size="small"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          prop="orderNo"
          label="订单编号"
          show-overflow-tooltip
          width="200"
        ></el-table-column>
        <el-table-column
          prop="memberName"
          label="会员"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="money"
          label="订单金额"
          :formatter="moneyFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="payMoney"
          label="支付金额"
          :formatter="payMoneyFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="paymentWay"
          label="支付方式"
          :formatter="payWayFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          :formatter="statusFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="payTime"
          label="支付时间"
          :formatter="payTimeFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          :formatter="createTimeFormatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="80">
          <template #default="{ row }">
            <el-link type="primary" :underline="false" icon="el-icon-view" @click="handleView(row)">查看</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <pagination
          v-show="tableData.total > 0"
          :total="tableData.total"
          :page.sync="tableData.current"
          :limit.sync="tableData.size"
          @pagination="search"
        />
      </div>
      <el-footer>
      </el-footer>
      <OrderShow :show.sync="orderVisible" :order="order" />
    </el-main>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination/index.vue";
import OrderShow from "@/views/commerce/order/OrderShow.vue";
import {viewOrder,pageOrder} from "@/api/commerce/order";
import {Status} from "@/utils/constant/orderStatus";
import {PayWay} from "@/utils/constant/payWay";
import {parseTime} from "@/utils/common";
import {ApiError} from "@/utils/errors";

export default {
  components: {Pagination,OrderShow},
  data() {
    return {
      Status,
      PayWay,
      orderVisible: false,
      order: null,
      searchAll: false,
      selection:[],
      searchModel:{
      },
      tableData: {
        records: [],
        total: 0,
        current: 1,
        size: 10
      }
    }
  },
  created() {
    this.search();
  },
  methods: {
    handleSelectionChange(selection){
      this.selection = selection;
    },
    handleView(row){
      const params = {id:row.id};
      viewOrder(params).then(res => {
        if (res.status == 200) {
          if (res.data.errors) {
            return this.$message.error(ApiError.msg(res.data.errors));
          }
          this.order = res.data.data;
          this.orderVisible = true;
        }
      });
    },
    search() {
      const params = {
        orderNo: this.searchModel.orderNo,
        memberName: this.searchModel.memberName,
        moneyStart: this.searchModel.moneyStart,
        moneyEnd: this.searchModel.moneyEnd,
        payMoneyStart: this.searchModel.payMoneyStart,
        payMoneyEnd: this.searchModel.payMoneyEnd,
        payWay: this.searchModel.payWay,
        status: this.searchModel.status,
        current: this.tableData.current,
        size: this.tableData.size
      };
      if(this.searchModel.payTime){
        params.payTimeStart = this.searchModel.payTime[0];
        params.payTimeEnd = this.searchModel.payTime[1];
      }
      if(this.searchModel.createTime){
        params.createTimeStart = this.searchModel.createTime[0];
        params.createTimeEnd = this.searchModel.createTime[1];
      }
      pageOrder(params).then(res => {
        if (res.status == 200) {
          if (!res.data.success) {
            return this.$message.error(ApiError.msg(res.data.message));
          }
          const data = res.data.data;
          this.tableData.records = data.records;
          this.tableData.total = data.total;
          this.tableData.current = data.current;
          this.tableData.size = data.size;
        }
      });
    },
    resetSearch(){
      this.searchModel.orderNo = null;
      this.searchModel.memberName = null;
      this.searchModel.moneyStart = null;
      this.searchModel.moneyEnd = null;
      this.searchModel.payMoneyStart = null;
      this.searchModel.payMoneyEnd = null;
      this.searchModel.payWay = null;
      this.searchModel.payTime = null;
      this.searchModel.status = null;
      this.searchModel.createTime = null;
    },
    moneyFormatter(row) {
      return row.money/100+'元';
    },
    payMoneyFormatter(row) {
      return row.payMoney/100+'元';
    },
    payWayFormatter(row) {
      return PayWay[row.payWay-1]["name"];
    },
    payTimeFormatter(row) {
      return parseTime(row["payTime"]);
    },
    statusFormatter(row) {
      return Status[row.status]["name"];
    },
    createTimeFormatter(row) {
      return parseTime(row["createTime"]);
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/full-form";
@import "@/styles/common-table";
</style>
